package de.hdu.tennis.ctrl;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import de.hdu.tennis.dao.MitgliedDao;

/**
 * HomeController
 */

@Controller
public class HomeController {
	
	private static Logger log = Logger.getLogger(HomeController.class);
	private MitgliedDao mitgliedDao = null; 
	private int start = 0; 
	
	
    /**
     * Custom handler for the home view.
     * @return View name "home" selecting view "/view/home.jsp"
     * @author Lilli Kardapolzew, Katharina Weber, Carolina Hirtreiter
     * @throws IOException 
     * @throws ServletException 
     */

	
	@RequestMapping("/index.html")
	public ModelAndView login(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
				
		if (start == 0) {
			log.info("home");
			HttpSession session = request.getSession(true);
			session.invalidate();
			ModelAndView mv = new ModelAndView();
			mv.addObject("title", "Herzlich Willkommen bei TennisHDU");
			mv.addObject("message", "Bitte loggen Sie sich ein.");
			mv.setViewName("home");
			return mv;
		}
		else
		{			
			log.info("home");			
			ModelAndView mv = new ModelAndView();
			mv.addObject("title", "Herzlich Willkommen, Sie sind eingeloggt!");
			mv.addObject("message", "Bitte loggen Sie sich ein.");
			mv.setViewName("welcome");
			return mv;
		}
		
	}	
	
	/**
	 * Custom handler for the welcome view
	 * @return View name "welcome" selecting view "/view/welcome.jsp"
     * @author Lilli Kardapolzew, Katharina Weber, Carolina Hirtreiter
     * @throws IOException 
     * @throws ServletException
	 */
	@RequestMapping("/home.html")
	public ModelAndView home(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{		
		HttpSession session = request.getSession(true);
		if (session.getAttribute("loggedin")!="true") {
			return login(request, response);
		}
		else
		{
			if(start == 0){
				log.info("home"); 
				start = 1;
			}
			log.info("home");			
			ModelAndView mv = new ModelAndView();
			mv.addObject("title", "Herzlich Willkommen, Sie sind eingeloggt!");
			mv.addObject("message", "Bitte loggen Sie sich ein.");
			mv.setViewName("welcome");
			return mv;
		}
		
	}	
	
	/**
	 * Custom handler for the home view, while logout 
	 * @return View name "home" selecting view "/view/home.jsp"
     * @author Lilli Kardapolzew, Katharina Weber, Carolina Hirtreiter
     * @throws IOException 
     * @throws ServletExceptio
	 */

	@RequestMapping("/logout.html")
	public ModelAndView logout(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException{
		log.info("welcome");
		ModelAndView mv = new ModelAndView();
		mv.addObject("title", "Herzlich Willkommen");
		HttpSession session = request.getSession(true);
		session.removeAttribute("loggedin");
		session.removeAttribute("mId");
		session.removeAttribute("mitglied");
		session.removeAttribute("isAdmin");
		session.invalidate();
		mv.addObject("message", "Bitte loggen Sie sich ein.");
		mv.setViewName("home");
		return mv;
	}


	public MitgliedDao getMitgliedDao() {
		return mitgliedDao;
	}


	public void setMitgliedDao(MitgliedDao mitgliedDao) {
		this.mitgliedDao = mitgliedDao;
	}

	public static Logger getLog() {
		return log;
	}

	public static void setLog(Logger log) {
		HomeController.log = log;
	}


}
